package com.design.twords.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.design.twords.R;
import com.design.twords.config.ConfigData;
import com.design.twords.config.ConstantValue;
import com.design.twords.database.UserConfig;

import org.litepal.LitePal;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Create by Alen
 * Time:  13:53
 */
public class RepeatSetActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "RepeatSetActivity";

    private ImageView back, bookimg;
    private TextView bookname, wordnumb;
    private EditText daywords;
    private Button save, drop;
    private RelativeLayout noplan, bookdetail;

    private int bookid;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repeatset);
        initView();
        initListener();
        initData();

    }

    private void initData() {
        List<UserConfig> configs = LitePal.where("userId = ?", ConfigData.getUserId()).find(UserConfig.class);
        if (configs.size() > 0 ){
            bookid = configs.get(0).getCurrentBookId();
            Log.d(TAG, "bookid" + bookid);
            bookimg.setImageResource(ConstantValue.bookPicById(bookid));
            bookname.setText(ConstantValue.bookNameById(bookid));
            wordnumb.setText(ConstantValue.wordTotalNumberById(bookid)+"");
            daywords.setText(configs.get(0).getDaywords()+"");
        }


    }

    private void initListener() {
        back.setOnClickListener(this);
        bookimg.setOnClickListener(this);
        save.setOnClickListener(this);
        drop.setOnClickListener(this);
    }

    private void initView() {
        back = findViewById(R.id.repeatset_back);
        bookimg = findViewById(R.id.repeatset_bookimg);
        bookname = findViewById(R.id.repeatset_bookname);
        wordnumb = findViewById(R.id.repeatset_wordnumb);
        daywords = findViewById(R.id.repeatset_daywords);
        save = findViewById(R.id.repeatset_save);
        drop = findViewById(R.id.repeatset_drop);

        noplan = findViewById(R.id.repeatset_noplan);
        bookdetail = findViewById(R.id.repeatset_bookdetail);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.repeatset_back:
                onBackPressed();
                finish();
                break;
            case R.id.repeatset_bookimg:
                intent.setClass(RepeatSetActivity.this, WordListActivity.class);
                intent.putExtra("floder_id", bookid);
                intent.putExtra("sources", "wordbooklist");
                startActivity(intent);
                break;
            case R.id.repeatset_save:
                UserConfig userConfig = new UserConfig();
                userConfig.setDaywords(Integer.parseInt(daywords.getText().toString()));
                userConfig.updateAll("userId = ?", ConfigData.getUserId());
                Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();
                break;
            case R.id.repeatset_drop:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("删除计划")
                        .setMessage("删除计划后,将重置背诵信息")
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                LitePal.deleteAll(UserConfig.class,"userId = ?", ConfigData.getUserId());
                                Toast.makeText(RepeatSetActivity.this,"删除成功", Toast.LENGTH_LONG).show();
                                noplan.setVisibility(View.VISIBLE);
                            }
                        })
                        .show();
                break;
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        List<UserConfig> configs = LitePal.where("userId = ?", ConfigData.getUserId()).find(UserConfig.class);
        if (configs.size() > 0){
            bookdetail.setVisibility(View.VISIBLE);
            noplan.setVisibility(View.GONE);
        }else {
            noplan.setVisibility(View.VISIBLE);
            bookdetail.setVisibility(View.GONE);
        }

    }
}
